package com.jingdianjichi.oss.controller;

import com.jingdianjichi.oss.adapter.FileService_adapter;
import com.jingdianjichi.oss.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@Slf4j
public class FileController {

    //使用适配器
    @Resource
    private FileService_adapter fileService_adapter;

    /**
     * 上传文件
     */
    @RequestMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<String> upload(@RequestPart("uploadFile") MultipartFile uploadFile, String bucket, String objectName) {
        try {
            String url = fileService_adapter.uploadFile(uploadFile, bucket, objectName);
            return Result.ok(url);
        }catch (Exception e){
            log.error("上传图片失败:{}",e.getMessage(),e);
            return Result.fail("上传图片失败");
        }
    }
}
